/***
 * 资源任务list
 */
var ResTaskList = new Pearlsen.BaseList({
	gridId:'resTaskGrid',
	advSearchId:'advSearchWin1',
	module:'资源任务',
	modulePath:'/pages/rms/restask/',
	moduleName:'ResTask',
	idProperty:'taskId',
	nameProperty:'resName',
	dataSetHandler:'ResTaskDataSetHandler',
	queryHandler:'ResTaskQueryHandler',
	renderStatus:function(value){
		var color=['green','green','red','red'];
		var arr=['未启动','已启动','已停止','已作废'];
		try{
			var intIndex = parseInt(value);
			return '<span style=\'color:'+color[intIndex]+';\'>'+arr[intIndex]+'</span>';
		}catch(e){
		}
        return "";
	},
	run:function(){
		var me = ResTaskList;
		var record = this.getSelectedOne();
		if(record){
			var taskId = record.data["taskId"];
			var applyId = record.data["applyId"];
			Ext.MessageBox.confirm('提示', '您确定要立即执行吗?',function(btn){
				if(btn=='yes'){
					Ext.Msg.wait("正在执行中,请稍后 ...",'',null);
			    	var dataSet = new DataSet(me.dataSetHandler,me.dataSetHandler);
			    	dataSet.setHandlerMethod("run");
			    	dataSet.newRecord();
			    	dataSet.setValue("taskId",taskId);
			    	dataSet.setValue("applyId",applyId);
			    	dataSet.execute(function(result){
			    		if(result.getStatus()=="200"){
			    			if( result.getRecords() && result.getRecords().length > 0 ){
				    			var record = result.getRecords()[0];
				    			var success = record["success"];
				    			var error = record["error"];
				    			if(success=="1"){
				    				Pearlsen.Utils.showSuccessMsg("执行成功");
				    			}else{
					    			Pearlsen.Utils.showErrorMsg('执行失败,原因:'+error);
				    			}
			    			}
			    		}else{
			    			Pearlsen.Utils.showErrorMsg('执行失败,原因:'+result.message);
			    		}
	    				Ext.Msg.hide();
			    	});
				}
			});
		}
	},
	unuse:function(){
		var me = ResTaskList;
		var record = this.getSelectedOne();
		if(record){
			var taskId = record.data["taskId"];
			var applyId = record.data["applyId"];
			Ext.MessageBox.confirm('提示', '您确定要停止此任务吗?',function(btn){
				if(btn=='yes'){
			    	var dataSet = new DataSet(me.dataSetHandler,me.dataSetHandler);
			    	dataSet.setHandlerMethod("stop");
			    	dataSet.newRecord();
			    	dataSet.setValue("taskId",taskId);
			    	dataSet.setValue("applyId",applyId);
			    	dataSet.execute(function(result){
			    		if(result.getStatus()=="200"){
		    				Pearlsen.Utils.showSuccessMsg("执行成功");
		    				record.set("status","2");
			    		}else{
			    			Pearlsen.Utils.showErrorMsg('执行失败,原因:'+result.message);
			    		}
			    	});
				}
			});
		}
	},
	view:function(){
		var record = this.getSelectedOne();
		if(record ){
			var objId = record.data[this.idProperty];
			var objName = record.data[this.nameProperty];
			var applyId = record.data["applyId"];
			var url = this.modulePath +"viewResTask.jsp?objId={0}&objName={1}&applyId={2}";
			url = url.format(objId,objName,applyId);
			var tabId = this.createTabId("add");
			parent.MainTab.addTab(tabId,"资源任务查看",url);
		}
	}
});
